/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.waste.gui;

import java.util.Collection;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BusinessObjectPanel;
import org.toools.gwt.widget.client.bc.StringAttribute;
import org.toools.gwt.widget.client.bop.IntegerAttribute;
import org.toools.gwt.wmc.jpa.Container;
import org.toools.gwt.wmc.jpa.Waste;

/**
 *
 * @author Manuel
 */
public class WastePanel extends BusinessObjectPanel {

    StringAttribute waste_desc = null;
    IntegerAttribute gtin = null;
    IntegerAttribute spec = null;
    StringAttribute criticality = null;
    StringAttribute spec_desc = null;
    StringAttribute critical_key = null;
    StringAttribute type_critical_key = null;
    StringAttribute non_critical_key = null;
    StringAttribute type_non_critical_key = null;
    StringAttribute comment = null;
//    DateAttribute valid_from = null;
//    DateAttribute valid_to = null;

    Collection<Container> parents = new Vector<Container>() {};

    public WastePanel(Object bp) {
        super(bp, 2);

        waste_desc = new StringAttribute("waste description", false);
        waste_desc.setMaxLength(50);
        gtin = new IntegerAttribute("gtin", false, Integer.MAX_VALUE);
        spec = new IntegerAttribute("spec", false, Integer.MAX_VALUE);
        criticality = new StringAttribute("criticality", false);
        criticality.setMaxLength(50);
        spec_desc = new StringAttribute("spec description", false);
        spec_desc.setMaxLength(50);
        critical_key = new StringAttribute("critical key", false);
        critical_key.setMaxLength(50);
        type_critical_key = new StringAttribute("type critical key", false);
        type_critical_key.setMaxLength(50);
        non_critical_key = new StringAttribute("non critical key", false);
        non_critical_key.setMaxLength(50);
        type_non_critical_key = new StringAttribute("type non critical key", false);
        type_non_critical_key.setMaxLength(50);
        comment = new StringAttribute("comment", false);
        comment.setMaxLength(200);
//        valid_from = new DateAttribute("valid from", false);
//        valid_to = new DateAttribute("valid to", false);

        this.add( gtin );
        this.add( waste_desc );
        this.add( spec );
        this.add( criticality );
        this.add( spec_desc );
        this.add( critical_key );
        this.add( type_critical_key );
        this.add( non_critical_key );
        this.add( type_non_critical_key );
        this.add( comment );
//        this.add( valid_from );
//        this.add( valid_to );

        recreateContent();
        preselect();
        validate();

        setStatus("* missing or invalid input");
    }

    public void addParent(Container parent) {
        this.parents.add(parent);
    }

    public void preselect() {
        if (getBusinessObject() != null) {
            Waste a = (Waste) getBusinessObject();
            gtin.setAttributeValue(a.getGtin());
            waste_desc.setAttributeValue(a.getWasteDesc());
            spec.setAttributeValue(a.getSpec());
            criticality.setAttributeValue(a.getCriticality());
            spec_desc.setAttributeValue(a.getSpecDesc());
            critical_key.setAttributeValue(a.getCritical_key());
            type_critical_key.setAttributeValue(a.getTypeCriticalKey());
            non_critical_key.setAttributeValue(a.getNonCriticalKey());
            type_non_critical_key.setAttributeValue(a.getTypeNonCriticalKey());
            comment.setAttributeValue(a.getComment());
//            valid_from.setAttributeValue(a.getValidFrom());
//            valid_to.setAttributeValue(a.getValidTo());
        } else {
            gtin.setAttributeValue("");
            waste_desc.setAttributeValue("");
            spec.setAttributeValue("");
            criticality.setAttributeValue("");
            spec_desc.setAttributeValue("");
            critical_key.setAttributeValue("");
            type_critical_key.setAttributeValue("");
            non_critical_key.setAttributeValue("");
            type_non_critical_key.setAttributeValue("");
            comment.setAttributeValue("");
//            valid_from.setAttributeValue("");
//            valid_to.setAttributeValue("");
        }
    }

    public void map() {
        if (getBusinessObject() == null) {
            setBusinessObject(new Waste());
        }
        Waste a = (Waste) getBusinessObject();
        a.setGtin(Integer.parseInt(gtin.getAttributeValue().toString()));
        a.setWasteDesc("" + waste_desc.getAttributeValue());
        a.setSpec(Integer.parseInt(spec.getAttributeValue().toString()));
        a.setCriticality("" + criticality.getAttributeValue());
        a.setSpecDesc("" + spec_desc.getAttributeValue());
        a.setCritical_key("" + critical_key.getAttributeValue());
        a.setTypeCriticalKey("" + type_critical_key.getAttributeValue());
        a.setNonCriticalKey("" + non_critical_key.getAttributeValue());
        a.setTypeNonCriticalKey("" + type_non_critical_key.getAttributeValue());
        a.setComment("" + comment.getAttributeValue());
//        DateFormat df = DateFormat.getDateInstance();
//        try {
//            a.setValidFrom(df.parse(valid_from.getAttributeValue().toString()));
//            a.setValidTo(df.parse(valid_to.getAttributeValue().toString()));
//        } catch (ParseException ex) {}
        if (parents.size() > 0) {
            a.setContainers(parents);
        }
    }

}
